<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>relativeTime | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>relativeTime</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>relativeTime</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/customization/relative-time/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/07-customization/07-relative-time.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">// &#x4ECE; 2.12.0 &#x5F00;&#x59CB;
moment.updateLocale(&apos;en&apos;, {
    relativeTime : Object
});
// &#x4ECE; 2.8.1 &#x81F3; 2.11.2
moment.locale(&apos;en&apos;, {
    relativeTime : Object
});

// &#x5E9F;&#x5F03;&#x4E8E; 2.8.1
moment.lang(&apos;en&apos;, {
    relativeTime : Object
});
</code></pre>

							</div>
<p><code>Locale#relativeTime</code> &#x5E94;&#x662F;&#x7528;&#x4E8E; <code>moment#from</code> &#x7684;&#x66FF;&#x6362;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="language-javascript">moment.updateLocale(&apos;en&apos;, {
    relativeTime : {
        future: &quot;in %s&quot;,
        past:   &quot;%s ago&quot;,
        s  : &apos;a few seconds&apos;,
        ss : &apos;%d seconds&apos;,
        m:  &quot;a minute&quot;,
        mm: &quot;%d minutes&quot;,
        h:  &quot;an hour&quot;,
        hh: &quot;%d hours&quot;,
        d:  &quot;a day&quot;,
        dd: &quot;%d days&quot;,
        M:  &quot;a month&quot;,
        MM: &quot;%d months&quot;,
        y:  &quot;a year&quot;,
        yy: &quot;%d years&quot;
    }
});
</code></pre>
<p><code>Locale#relativeTime.future</code> &#x6307;&#x5411;&#x672A;&#x6765;&#x65E5;&#x671F;&#x7684;&#x524D;&#x7F00;/&#x540E;&#x7F00;&#xFF0C;&#x800C; <code>Locale#relativeTime.past</code> &#x5219;&#x6307;&#x5411;&#x8FC7;&#x53BB;&#x65E5;&#x671F;&#x7684;&#x524D;&#x7F00;/&#x540E;&#x7F00;&#x3002;
&#x5BF9;&#x4E8E;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x5B57;&#x7B26;&#xFF0C;&#x5355;&#x4E2A;&#x5B57;&#x7B26;&#x6307;&#x5411;&#x5355;&#x6570;&#xFF0C;&#x800C;&#x53CC;&#x5B57;&#x7B26;&#x6307;&#x5411;&#x590D;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x9700;&#x8981;&#x5BF9;&#x4EE4;&#x724C;&#x8FDB;&#x884C;&#x5176;&#x4ED6;&#x5904;&#x7406;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x7B7E;&#x540D;&#x5C06;&#x4EE4;&#x724C;&#x8BBE;&#x7F6E;&#x4E3A;&#x51FD;&#x6570;&#x3002;
&#x8BE5;&#x51FD;&#x6570;&#x5E94;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">function (number, withoutSuffix, key, isFuture) {
    return string;
}
</code></pre>
<p><code>key</code> &#x53C2;&#x6570;&#x6307;&#x5411; <code>Locale#relativeTime</code> &#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x66FF;&#x6362;&#x952E;&#x3002;&#xFF08;&#x4F8B;&#x5982; <code>s m mm h</code> &#x7B49;&#xFF09;</p>
<p><code>number</code> &#x53C2;&#x6570;&#x6307;&#x5411;&#x8BE5;&#x952E;&#x7684;&#x5355;&#x4F4D;&#x6570;&#x3002;
&#x5BF9;&#x4E8E; <code>m</code>&#xFF0C;&#x8BE5;&#x6570;&#x5B57;&#x662F;&#x5206;&#x949F;&#x6570;&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E0D;&#x5E26;&#x540E;&#x7F00;&#x663E;&#x793A;&#x4EE4;&#x724C;&#xFF0C;&#x5219; <code>withoutSuffix</code> &#x53C2;&#x6570;&#x5C06;&#x4F1A;&#x4E3A; true&#xFF0C;&#x5982;&#x679C;&#x5E26;&#x540E;&#x7F00;&#x663E;&#x793A;&#x4EE4;&#x724C;&#xFF0C;&#x5219;&#x4E3A; false&#x3002;&#xFF08;&#x4E4B;&#x6240;&#x4EE5;&#x4F7F;&#x7528;&#x903B;&#x8F91;&#x5012;&#x7F6E;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x663E;&#x793A;&#x540E;&#x7F00;&#x3002;&#xFF09;</p>
<p>&#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;&#x672A;&#x6765;&#x7684;&#x540E;&#x7F00;/&#x524D;&#x7F00;&#xFF0C;&#x5219; <code>isFuture</code> &#x53C2;&#x6570;&#x5C06;&#x4F1A;&#x4E3A; true&#xFF0C;&#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;&#x8FC7;&#x53BB;&#x7684;&#x524D;&#x7F00;/&#x540E;&#x7F00;&#xFF0C;&#x5219;&#x4E3A; false&#x3002;</p>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">// From 2.12.0 onward
moment.updateLocale(&apos;en&apos;, {
    relativeTime : Object
});
// From 2.8.1 to 2.11.2
moment.locale(&apos;en&apos;, {
    relativeTime : Object
});

// Deprecated in 2.8.1
moment.lang(&apos;en&apos;, {
    relativeTime : Object
});
</code></pre>

							</div>
<p><code>Locale#relativeTime</code> should be an object of the replacement strings for <code>moment#from</code>.</p>
<pre><code class="language-javascript">moment.updateLocale(&apos;en&apos;, {
    relativeTime : {
        future: &quot;in %s&quot;,
        past:   &quot;%s ago&quot;,
        s  : &apos;a few seconds&apos;,
        ss : &apos;%d seconds&apos;,
        m:  &quot;a minute&quot;,
        mm: &quot;%d minutes&quot;,
        h:  &quot;an hour&quot;,
        hh: &quot;%d hours&quot;,
        d:  &quot;a day&quot;,
        dd: &quot;%d days&quot;,
        M:  &quot;a month&quot;,
        MM: &quot;%d months&quot;,
        y:  &quot;a year&quot;,
        yy: &quot;%d years&quot;
    }
});
</code></pre>
<p><code>Locale#relativeTime.future</code> refers to the prefix/suffix for future dates, and <code>Locale#relativeTime.past</code> refers to the prefix/suffix for past dates. For all others, a single character refers to the singular, and a double character refers to the plural.</p>
<p>If a locale requires additional processing for a token, it can set the token as a function with the following signature.
The function should return a string.</p>
<!-- skip-example -->
<pre><code class="language-javascript">function (number, withoutSuffix, key, isFuture) {
    return string;
}
</code></pre>
<p>The <code>key</code> argument refers to the replacement key in the <code>Locale#relativeTime</code> object. (eg. <code>s m mm h</code>, etc.)</p>
<p>The <code>number</code> argument refers to the number of units for that key. For <code>m</code>, the number is the number of minutes, etc.</p>
<p>The <code>withoutSuffix</code> argument will be true if the token will be displayed without a suffix, and false if it will be displayed with a suffix. (The reason for the inverted logic is because the default behavior is to display with the suffix.)</p>
<p>The <code>isFuture</code> argument will be true if it is going to use the future suffix/prefix and false if it is going to use the past prefix/suffix.</p>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>